//
//  THJCompanyIntroVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/12/11.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJCompanyIntroVC.h"
#import "UIButton+ImageTitleSpacing.h"

@interface THJCompanyIntroVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *companyInfoTF;
@property (weak, nonatomic) IBOutlet UIImageView *companyImgV;

@end

@implementation THJCompanyIntroVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"编辑企业简介"];
    }else{
        [self setNavBarWithTitle:@"编辑企业简介"];
    }
    [self.photoBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
}
- (IBAction)changePhotoAction:(id)sender {
    NSLog(@"点击修改图片");
    [self alertWithTitle1:@"取消" title2:@"拍照" title3:@"相册"];
}

- (IBAction)sureBtnAction:(id)sender {
    NSLog(@"确认修改");
    
    if(!self.companyInfoTF.text.length || [self.companyInfoTF.text isEqual:@"请输入公司简介"]){
        [self HUDShowMsg:@"请输入简介" AfterDelayDimiss:1];
    }else{
        NSString * imgDataStr = [self image2DataURL:self.companyImgV.image];
        [self admitDataWithImgStr:imgDataStr companyInfoStr:self.companyInfoTF.text];
    }
}
#pragma mark ----更多操作---
-(void)actionSheetIndex0Function{
    NSLog(@"取消");
}
-(void)actionSheetIndex1Function{
    [self getPhotoWithType:0];
}
-(void)actionSheetIndex2Function{
    [self getPhotoWithType:1];
}
/**
 调用相册或相机
 */
-(void)getPhotoWithType:(NSInteger)index{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (index) {
            case 0:
                //来源:相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                //来源:相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.navigationBar.translucent = NO;
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.companyImgV.image = image;
}
-(void)admitDataWithImgStr:(NSString*)imgStr companyInfoStr:(NSString*)str{
    NSDictionary * dic = @{@"banner":imgStr,@"content":str};
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=My_company&r=My_company&m=info"];
    [THJNetWork postDataFromUrl:url parameters:dic Token:token callback:^(NSDictionary* receiveData, NSError *error){
        NSLog(@"上传简介%@",receiveData);
        [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [self HUDShowMsg:@"上传成功" AfterDelayDimiss:1];
            [self performSelector:@selector(pop) withObject:nil afterDelay:1];
        }
    }];
}
@end
